var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = require('../../../app.js');
var user = require('../../../services/user.js');
var wxcache = require('../../../utils/wxcache.js');


var app = getApp();

Page({
  data: {
    coupon_info:null,
    selectCouponId:''
  },
  couponCode:'',
  couponShowStatus: ['ACTIVATED','EXPIRED'],
  beforePageFlag: 1,


  onLoad: function (options) {
    //加载优惠券
    this.getAllCoupon();
    if (options.couponId) {
      this.setData({
        selectCouponId: options.couponId
      })
    }
    if (options.frompage == 'order') {
      this.beforePageFlag = 1
      
    }else{
      this.beforePageFlag = 0
    }
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  },

  getAllCoupon: function () {
    //获取用户所有优惠券
    var that =this;
    user.checkLogin().then(res => {
        //用户已登录
        var uid = wxcache.get("userInfo").id
        util.request(api.CouponsQuery).then(function (res) {
          console.log(res)
          var couponList = []
          if (that.beforePageFlag == 1){
              res.responseBody.userCouponVoList.forEach((coupon,index)=>{
                
                if (coupon.status == 'ACTIVATED'){
                  couponList.push(coupon)
                }
              }) 

          }else{
              res.responseBody.userCouponVoList.forEach((coupon,index)=>{
                
                if (that.couponShowStatus.indexOf(coupon.status)>=0){
                  couponList.push(coupon)
                }
              })  
          }
          

          that.setData({
            coupon_info: couponList
          });
        })
    }).catch(() => {
        //用户未登录
        wx.showModal({
          title: '',
          content: '请登录小程序',
          showCancel: false
        });
    });
  },
 
  useCoupon:function(e){
    
     console.log(e);
     var c_info = e.currentTarget.dataset.info;

    if (this.beforePageFlag == 1){
     
      
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2]; //上一个页面
      //直接调用上一个页面的setData()方法，把数据存到上一个页面中去

      if(c_info.status == 'ACTIVATED'){

        if (prevPage.data.coupon_1_select_id == c_info.seqNo){

            prevPage.setData({
              youhuiquanliename: "未选择",
              coupon_1_select_id: '',
              coupon_1_select_coupontype:'',
              youhuiquanliePrice:''
            })


          }else{
            prevPage.setData({
              youhuiquanliename: c_info.name + "(￥" + c_info.value + ")",
              coupon_1_select_id: c_info.seqNo,
              coupon_1_select_coupontype: c_info.couponId,
              youhuiquanliePrice: c_info.value
            })
          }
          
          prevPage.orderTrial();
          wx.navigateBack()
      }
      
    }else{
      if(c_info.status == "ACTIVATED"){
        wx.switchTab({
        url: '/pages/index/index',
        })
      }
      
    }

  },

  exchange:function(e){
    let that = this;

    if (!that.couponCode) {
      wx.showModal({
        title: '',
        content: '优惠码不能为空',
        showCancel:false
      })
      return
    }
    util.request(api.ExchangeCoupon + that.couponCode,{},"POST").then(function(res){
      console.log("res",res)
      if (res.code == 200){
        wx.showToast({
          title: '兑换成功',
          icon: 'succes',
          duration: 1500,
          mask: true
        })
      }else{
        wx.showModal({
          title: '',
          content: '优惠码无效',
          showCancel: false
        })
      }
      that.getAllCoupon()
    });
  },
  couponInput:function(e){
    console.log(e)
    var couponCode = e.detail.value
    this.couponCode = couponCode
  },
  openCouponIntroduce:function(){
    wx.navigateTo({
      url: '/pages/ucenter/couponIntroduce/couponIntroduce',
    })
  }
})